<template>
  <transition
    name="animate__animated animate__bounce"
    appear
    enter-active-class="animate__backInDown"
    leave-active-class="animate__zoomOut"
  >
    <div class="manage">
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <!-- 用户的表单信息 -->
        <el-form
          :rules="rules"
          :inline="true"
          ref="ruleForm"
          :model="ruleForm"
          label-width="80px"
          status-icon
        >
          <!-- 姓名 -->
          <el-form-item label="姓名" prop="name">
            <el-input
              placeholder="请输入姓名"
              v-model="ruleForm.name"
            ></el-input>
          </el-form-item>
          <!-- 年龄 -->
          <el-form-item label="年龄" prop="age">
            <el-input
              placeholder="请输入年龄"
              v-model.number="ruleForm.age"
            ></el-input>
          </el-form-item>
          <!-- 性别 -->
          <el-form-item label="性别" prop="sex">
            <el-select v-model="ruleForm.sex" placeholder="请选择您的性别">
              <el-option label="男" :value="1"></el-option>
              <el-option label="女" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <!-- 出生日期 -->
          <el-form-item label="您的生日" prop="birth">
            <el-col :span="11">
              <el-date-picker
                type="date"
                placeholder="选择日期"
                v-model="ruleForm.birth"
                style="width: 170%"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </el-col>
          </el-form-item>
          <!-- 地址 -->
          <el-form-item label="地址" prop="addr">
            <el-input
              placeholder="请输入地址"
              v-model="ruleForm.addr"
            ></el-input>
          </el-form-item>
        </el-form>

        <!-- 弹出的对话框的按钮 -->
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="cancel">关闭</el-button>
        </span>
      </el-dialog>
      <div class="manage-header">
        <el-button type="primary" @click="handleAdd"> +新增 </el-button>
        <!-- from搜索区 -->
        <el-form :inline="true" :model="userfrom">
          <el-form-item label="搜索">
            <el-input
              v-model="userfrom.name"
              placeholder="请输入要搜索的名字"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="common-table">
        <!-- 表格 -->
        <el-table
          :data="tableData"
          style="width: 100%"
          border
          height="530"
          :stripe="true"
        >
          <el-table-column prop="name" label="姓名"> </el-table-column>
          <el-table-column prop="sex" label="性别">
            <template slot-scope="scope">
              <i class="el-icon-time"></i>
              <span style="margin-left: 10px">{{
                scope.row.sex === 1 ? "男" : "女"
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="age" label="年龄"> </el-table-column>
          <el-table-column prop="birth" label="出生日期"> </el-table-column>
          <el-table-column prop="addr" label="地址"> </el-table-column>
          <el-table-column prop="addr" label="操作">
            <template slot-scope="scope">
              <el-button type="" size="mini" @click="handleEdit(scope.row)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                size="mini"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <Tablepet />
        <div class="pager">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            @current-change="handlePage"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import { addUser, delUser, editUser, getUser } from "../api/";
import Tablepet from "./Tablepet.vue";
export default {
  name: "User",
  data() {
    return {
      //当为false时则关闭弹窗，否则显示
      dialogVisible: false,
      //表单数据
      ruleForm: {
        name: "",
        age: "",
        sex: "",
        birth: "",
        addr: "",
      },
      //校验规则
      rules: {
        name: [
          { required: true, message: "请输入您的姓名", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
        age: [
          { required: true, message: "请输入您的年龄", trigger: "blur" },
          { type: "number", message: "年龄必须为数字值" },
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "change" }],
        birth: [
          {
            type: "string",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        addr: [{ required: true, message: "请填您的地址", trigger: "blur" }],
      },
      //表格数据
      tableData: [],
      modelType: 0, //0表示新增的弹窗，1表示编辑
      total: 0, //当前的总条数
      pageData: {
        page: 1,
        limit: 10,
      },
      userfrom: {
        name: "",
      },
    };
  },
  methods: {
    //点x关闭弹窗
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
      this.$refs.ruleForm.resetFields();
    },
    //提交用户表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.modelType === 0) {
            addUser(this.ruleForm).then(() => {
              //重新获取列表的接口
              this.getList();
              this.$refs[formName].resetFields();
            });
            this.$refs[formName].resetFields();
          } else {
            editUser(this.ruleForm).then(() => {
              //重新获取列表的接口
              this.getList();
            });
          }
          this.dialogVisible = false;

          this.$message({
            type: "success",
            message: "提交成功!",
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //点击按钮关闭弹窗
    cancel() {
      this.$confirm("确认关闭？").then(() => {
        this.$refs.ruleForm.resetFields();
        this.dialogVisible = false;
      });
    },
    //编辑一行数据
    handleEdit(row) {
      this.modelType = 1;
      this.dialogVisible = true;
      //注意需要对当前进行数据深拷贝，否则会报错
      this.ruleForm = JSON.parse(JSON.stringify(row));
    },
    //删除一行数据
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUser({ id: row.id }).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //获取列表的数据
    getList() {
      //获取数据列表
      getUser({ params: { ...this.userfrom, ...this.pageData } }).then(
        (data) => {
          console.log(data.data.list);
          this.tableData = data.data.list;
          this.total = data.data.count || 0;
        }
      );
    },
    //添加
    handleAdd() {
      this.modelType = 0;
      this.dialogVisible = true;
    },
    //当我们选择页码的回调函数
    handlePage(val) {
      this.pageData.page = val;
      this.getList();
    },
    //列表查询的条件
    onSubmit() {
      this.getList();
    },
  },
  mounted() {
    this.getList();
  },
  components: {
    Tablepet,
  },
};
</script>

<style lang="less" scoped>
.manage {
  height: 90%;
  .manage-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .common-table {
    position: relative;
    .pager {
      position: absolute;
      bottom: 0;
      right: 20px;
      top: 550px;
    }
  }
}
</style>
